package com.genesis.yunnanji.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.genesis.yunnanji.R;
import com.genesis.yunnanji.adapter.DiZhouPlayAdapter;
import com.genesis.yunnanji.bean.DiZhouDetailsBean;
import com.genesis.yunnanji.bean.DiZhouPlayBean;
import com.genesis.yunnanji.config.GenesisApiConfig;
import com.genesis.yunnanji.utils.NetWorkUtils;
import com.google.gson.Gson;
import com.timmy.tdialog.TDialog;
import com.timmy.tdialog.base.BindViewHolder;
import com.timmy.tdialog.listener.OnBindViewListener;
import com.timmy.tdialog.listener.OnViewClickListener;

import org.json.JSONObject;
import org.xutils.http.RequestParams;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

/**
 * 创建日期：2018/10/31 0031
 * 作者：Kite
 * 描述：
 */
public class DiZhouDetailsActivity extends BaseActivity {

    @ViewInject(R.id.toolbarLayout)
    CollapsingToolbarLayout toolbarLayout;

    @ViewInject(R.id.toolbar)
    Toolbar toolbar;

    @ViewInject(R.id.tvIntroduce)
    TextView tvIntroduce;

    @ViewInject(R.id.tvEnvironment)
    TextView tvEnvironment;

    @ViewInject(R.id.rvPlay)
    RecyclerView rvPlay;

    private String mIntroduce = "";

    private String mEnvironment = "";

    private DiZhouPlayAdapter mDiZhouPlayAdapter;

    private List<DiZhouPlayBean> mList = new ArrayList<>();

    private Intent intent;

    private int[] diZhouPlayImg = new int[]{R.drawable.state_specialty, R.drawable.state_delicacy, R.drawable.state_mushroom,
            R.drawable.state_culture, R.drawable.state_scenic, R.drawable.state_estate, R.drawable.state_localprice,
            R.drawable.state_hotel, R.drawable.state_transport};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState, R.layout.activity_di_zhou_details);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        initList();
        toolbar.setNavigationIcon(R.mipmap.return_white);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected void setUpView() {
        Intent intent = getIntent();
        String id = intent.getStringExtra("id");
        initNetDate(id);
    }

    @Event(value = {R.id.tvIntroduce, R.id.tvEnvironment}, type = View.OnClickListener.class)
    private void click_something(View v) {
        switch (v.getId()) {
            case R.id.tvIntroduce:
                if (!TextUtils.isEmpty(mIntroduce)) {
                    initDialog(mIntroduce);
                }
                break;
            case R.id.tvEnvironment:
                if (!TextUtils.isEmpty(mEnvironment)) {
                    initDialog(mEnvironment);
                }
                break;
        }
    }

    private void initNetDate(final String id) {
        RequestParams params = new RequestParams(GenesisApiConfig.HOST + GenesisApiConfig.DIZHOU_NEW_DETAILS);
        params.addBodyParameter("id", id);
        NetWorkUtils.doCacheGet(params, new NetWorkUtils.HttpResultListener() {
            @Override
            public void onSuccess(JSONObject jsonObject) {
                Gson gson = new Gson();
                DiZhouDetailsBean bean = gson.fromJson(jsonObject.toString(), DiZhouDetailsBean.class);
                toolbarLayout.setTitle(bean.getResult().getInfo().getTitle());
                SimpleTarget<Drawable> simpleTarget = new SimpleTarget<Drawable>() {
                    @Override
                    public void onResourceReady(Drawable resource, Transition<? super Drawable> transition) {
                        toolbarLayout.setBackground(resource);
                    }
                };
                Glide.with(DiZhouDetailsActivity.this).load(GenesisApiConfig.PIC_HOST + bean.getResult().getInfo().getApp_img()).into(simpleTarget);
                mIntroduce = bean.getResult().getInfo().getDescription();
                mEnvironment = bean.getResult().getInfo().getDili();
                tvIntroduce.setText(bean.getResult().getInfo().getDescription());
                tvEnvironment.setText(bean.getResult().getInfo().getDili());
                mDiZhouPlayAdapter = new DiZhouPlayAdapter(DiZhouDetailsActivity.this, mList);
                rvPlay.setLayoutManager(new GridLayoutManager(DiZhouDetailsActivity.this, 4));
                rvPlay.setAdapter(mDiZhouPlayAdapter);
                mDiZhouPlayAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                        switch (position) {
                            case 0:
                                intent = new Intent(DiZhouDetailsActivity.this, SingleRecyclerActivity.class);
                                intent.putExtra("thing", "techan");
                                intent.putExtra("id", id);
                                startActivity(intent);
                                break;
                            case 1:
                                intent = new Intent(DiZhouDetailsActivity.this, SingleRecyclerActivity.class);
                                intent.putExtra("thing", "meishi");
                                intent.putExtra("id", id);
                                startActivity(intent);
                                break;
                            case 2:
                                intent = new Intent(DiZhouDetailsActivity.this, SingleRecyclerActivity.class);
                                intent.putExtra("thing", "yeshengjun");
                                intent.putExtra("id", id);
                                startActivity(intent);
                                break;
                            case 3:
                                intent = new Intent(DiZhouDetailsActivity.this, SingleRecyclerActivity.class);
                                intent.putExtra("thing", "feiyi");
                                intent.putExtra("id", id);
                                startActivity(intent);
                                break;
                            case 4:
                                intent = new Intent(DiZhouDetailsActivity.this, SingleRecyclerActivity.class);
                                intent.putExtra("thing", "jingdian");
                                intent.putExtra("id", id);
                                startActivity(intent);
                                break;
                            case 5:
                                String url = "";
                                intent = new Intent(DiZhouDetailsActivity.this, WebActivity.class);
                                if (genesisUtils.isLogin()) {
                                    url = "http://m.jinzhanji.com/Wenlv/index.html" + GenesisApiConfig.FROM_APP + GenesisApiConfig.APP_TOKEN + genesisUtils.getAppToken();
                                } else {
                                    url = "http://m.jinzhanji.com/Wenlv/index.html" + GenesisApiConfig.FROM_APP;
                                }
                                intent.putExtra("url", url);
                                startActivity(intent);
                                break;
                            case 6:
                                intent = new Intent(DiZhouDetailsActivity.this, SingleRecyclerActivity.class);
                                intent.putExtra("thing", "fangjia");
                                intent.putExtra("id", id);
                                startActivity(intent);
                                break;
                            case 7:
                                intent = new Intent(DiZhouDetailsActivity.this, SingleRecyclerActivity.class);
                                intent.putExtra("thing", "mingsu");
                                intent.putExtra("id", id);
                                startActivity(intent);
                                break;
                            case 8:
                                intent = new Intent(DiZhouDetailsActivity.this, SingleRecyclerActivity.class);
                                intent.putExtra("thing", "trip");
                                intent.putExtra("id", id);
                                startActivity(intent);
                                break;
                        }
                    }
                });
            }

            @Override
            public void OnCached(JSONObject jsonObject) {
            }

            @Override
            public void onFinished() {
            }

            @Override
            public void onError(String errorStr) {

            }
        });
    }

    private void initDialog(final String str) {
        new TDialog.Builder(getSupportFragmentManager())
                .setLayoutRes(R.layout.dialog_text)    //设置弹窗展示的xml布局
//                .setDialogView(view)  //设置弹窗布局,直接传入View
//                .setWidth(LayoutParams.MATCH_PARENT)  //设置弹窗宽度(px)
//                .setHeight(LayoutParams.WRAP_CONTENT)  //设置弹窗高度(px)
                .setScreenWidthAspect(this, 1f)   //设置弹窗宽度(参数aspect为屏幕宽度比例 0 - 1f)
                .setScreenHeightAspect(this, 0f)  //设置弹窗高度(参数aspect为屏幕宽度比例 0 - 1f)
                .setGravity(Gravity.BOTTOM)     //设置弹窗展示位置
                .setDimAmount(0.6f)     //设置弹窗背景透明度(0-1f)
                .setCancelableOutside(true)     //弹窗在界面外是否可以点击取消
                .setDialogAnimationRes(android.R.style.Animation_InputMethod)  //设置弹窗动画
                .setOnDismissListener(new DialogInterface.OnDismissListener() { //弹窗隐藏时回调方法
                    @Override
                    public void onDismiss(DialogInterface dialog) {
                    }
                })
                .setOnBindViewListener(new OnBindViewListener() {   //通过BindViewHolder拿到控件对象,进行修改
                    @Override
                    public void bindView(BindViewHolder bindViewHolder) {
                        TextView textView = bindViewHolder.getView(R.id.tvInfo);
                        textView.setText(str);
                    }
                })
                .addOnClickListener(R.id.tvInfo, R.id.ivClose)
                .setOnViewClickListener(new OnViewClickListener() {     //View控件点击事件回调
                    @Override
                    public void onViewClick(BindViewHolder viewHolder, View view, TDialog tDialog) {
                        switch (view.getId()) {
                            case R.id.tvInfo:
//                                tDialog.dismiss();
                                break;
                            case R.id.ivClose:
                                tDialog.dismiss();
                                break;
                        }
                        tDialog.dismiss();
                    }
                })
                .create()   //创建TDialog
                .show();    //展示
    }

    private void initList() {
        String[] diZhouPlayText = getResources().getStringArray(R.array.diZhouPlayText);
        for (int i = 0; i < diZhouPlayImg.length; i++) {
            mList.add(new DiZhouPlayBean(diZhouPlayImg[i], diZhouPlayText[i]));
        }
    }


}
